Chapter 5: User - Defined Methods

Complete Visual Guide, Flowcharts, and Line-by-Line Execution Simulators

1. Method Declaration, Syntax & Prototype

A method is a named block of code designed to perform a specific task. The Method Prototype (or Signature) is the first line of the method that tells the compiler everything it needs to know about how to interact with it.

[Access Specifier] [Modifier] [Return Type] MethodName( [Parameter List] )

Example: public static boolean checkSpy( int n )

Method Definition vs Method Calling

2. Static vs Non-Static & Object Creation

Static Methods

  • Belong to the Class itself, not an object.
  • Can be called directly using the Class name.
  • Memory: Shared across the entire program.
  • Example: Math.pow(2, 3);

Non-Static (Instance) Methods

  • Belong to a specific Object.
  • Requires Object Creation: ClassName obj = new ClassName(); to be invoked.
  • Memory: Each object has its own copy of instance variables modified by these methods.
  • Example: scanner.nextInt();
graph TD A[Method Invocation Required] --> B{Is Method Static?} B -->|Yes| C[Call directly: ClassName.methodName()] B -->|No| D[Create Object: ClassName obj = new ClassName();] D --> E[Call via Object: obj.methodName()]

3. Parameters: Actual vs Formal & Call by Value

Parameters are the mechanism for passing data into methods.

sequenceDiagram participant Main Program participant Method Memory Note over Main Program: int x = 50; Main Program->>Method Memory: Call Method(x) (Actual Parameter) Note over Method Memory: int a = 50; (Formal Parameter) Method Memory-->>Method Memory: a = a + 10; (a is now 60) Note over Main Program: x is STILL 50. Note over Main Program, Method Memory: This is CALL BY VALUE. A copy is passed, original remains unchanged.

Note on Call by Reference: Java strictly uses Call by Value. However, when you pass an Object (like an Array), you pass a copy of the reference (memory address). Modifying the array inside the method will affect the original array. This simulates call by reference.

4. Method Overloading & Flow Resolution

Method Overloading is when a class has multiple methods with the exact same name, but different parameter lists (different number, type, or order of parameters). It is an example of compile-time polymorphism.

graph LR A[Call: calculateArea(5.0)] --> B{Compiler checks signature} B --> C[Method: int calculateArea(int side)] B --> D[Method: double calculateArea(double radius)] B --> E[Method: int calculateArea(int l, int b)] D --> F((Match Found! Executes Double Version)) style D fill:#bbf7d0,stroke:#16a34a,stroke-width:2px style F fill:#10b981,color:white

5. Interactive Dry Run: Spy Number Logic

Let's dry run a practical program. A Spy Number is a number where the sum of its digits equals the product of its digits. Watch how local variables change in the Variable Description Table as the compiler loops through the formal parameter.

public static void main(String[] args) {
int num = 1124;
boolean isSpy = checkSpy(num);
System.out.println("Is Spy? " + isSpy);
}
public static boolean checkSpy(int n) {
int sum = 0;
int prod = 1;
while (n > 0) {
int d = n % 10;
sum = sum + d;
prod = prod * d;
n = n / 10;
}
return sum == prod;
}
Ready to execute. We start at the main method.
Variable Description Table
VariableCurrent Value
No variables initialized.

6. Pure and Impure Methods

Pure Methods (Accessor)

Do NOT change the internal state (instance variables) of an object. They simply return data or perform calculations.

Real world: Checking the time on a clock. You get the data, but you don't change the clock.

String.length()

Impure Methods (Mutator)

They CHANGE the internal state (instance variables) of an object.

Real world: Winding the clock or setting a new time. You have mutated the object's state.

Arrays.sort(arr)